﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VipCo.Data;
using VipCo.Entities;
using System.Data;

public partial class Admin_TheHoiVien_QLNhapThe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {

        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsMaThe = DataRepository.Provider.ExecuteDataSet("MaTheHv_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsMaThe.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsMaThe.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLNhapThe.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            MaTheHv entMaTheHv = new MaTheHv();
            entMaTheHv.Id = Convert.ToInt32(H_CatalogId.Value);
            entMaTheHv.MaThe = txtTen.Text;

            if (entMaTheHv.Id == 0)
            {
                entMaTheHv.Id = 0;
                updateStatus = DataRepository.MaTheHvProvider.Insert(entMaTheHv);
            }
            else
            {
                updateStatus = DataRepository.MaTheHvProvider.Update(entMaTheHv);
            }
            if (updateStatus)
            {
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                WebMessage.Show("Mã thẻ bị trùng!");
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLNhapThe.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Mã thẻ thất bại!");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                MaTheHv entMaTheHv = DataRepository.MaTheHvProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entMaTheHv != null)
                {
                    txtTen.Text = entMaTheHv.MaThe;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLNhapThe.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdMaTheHv = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.MaTheHvProvider.Delete(IdMaTheHv);
                if (!deleteStatus)
                    WebMessage.Show("Xóa Mã thẻ thất bại");
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLNhapThe.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                WebMessage.Show("Xóa Mã thẻ thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtTen.Text = "";

        H_CatalogId.Value = "0";
    }
}